<?php
/**
 * 手机招车
 */
namespace app\mobile\controller;

use dragon\Taxi\TaxiCall;
use think\Validate;

class TaxiController extends BaseController
{
    // 手机招车列表
    public function index()
    {
        $map = [];
        $list = TaxiCall::where($map)->order('update_time desc')->paginate($this->perpage);

        foreach($list as $key => $val) {
            $list[$key]['finished_time_format'] = time_format('m-d H:i', $val['finished_time']);
            $list[$key]['dispatch_time_format'] = time_format('m-d H:i', $val['dispatch_time']);
            $list[$key]['create_time_format'] = time_format('m-d H:i', $val['create_time']);
            $list[$key]['update_time_format'] = time_format('m-d H:i', $val['update_time']);
            $list[$key]['status_format'] = dict_convert('taxi_call_status', $val['status']);
        }

        return $this->success($list);
    }

    // 招车申请
    public function apply() {
        $validate = new Validate([
            'start'  => 'require',
            'end' => 'require',
            'num' => 'require',
            'start_time' => 'require',
        ]);
        $validate->message([
            'start.require' => '请选择起点',
            'end.require' => '请选择终点',
            'num.require' => '请输入乘客人数',
            'start_time.require' => '请选择出行时间',
        ]);

        $data = $this->request->post();
        if (!$validate->check($data)) {
            return $this->error($validate->getError());
        }

        if(!$this->user['mobile'] || !$this->user['user_realname']) return $this->error('请完善手机号及真实姓名');

        $data['user_name'] = $this->user['user_realname'];
        $data['user_mobile'] = $this->user['mobile'];
        $data['user_id']    = $this->user['id'];
        $data['order_no']   = system_no('taxi_call');

        $taxiCall = TaxiCall::create($data);

        return $this->success($taxiCall['id']);
    }

    // 未完成运单
    public function unfinished() {
        $info = TaxiCall::get([
            'user_id' => $this->user['id'],
            'status'  => ['in', [1, 2]],
        ]);

        return $this->success($info ? $info['id'] : 0);
    }

    // 招车详情
    public function info() {
        $info = TaxiCall::get($this->request->param('id'));

        $info['finished_time_format'] = time_format('m-d H:i', $info['finished_time']);
        $info['dispatch_time_format'] = time_format('m-d H:i', $info['dispatch_time']);
        $info['create_time_format'] = time_format('m-d H:i', $info['create_time']);
        $info['update_time_format'] = time_format('m-d H:i', $info['update_time']);
        $info['status_format'] = dict_convert('taxi_call_status', $info['status']);

        return $this->success($info);
    }

    // 完成运单
    public function finish() {
        $taxiCall = TaxiCall::get($this->request->param('id'));
        $taxiCall->save(['status' => 3,]);
        return $this->success('操作成功');
    }

    // 招车详情
    public function cancel() {
        $taxiCall = TaxiCall::get($this->request->param('id'));
        $taxiCall->save(['status' => 0]);
        return $this->success('操作成功');
    }
}
